<template>
	<xn-form-container
		:title="formData.id ? '编辑大模型对话记录表' : '增加大模型对话记录表'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="大模型配置ID：" name="configId">
				<a-input v-model:value="formData.configId" placeholder="请输入大模型配置ID" allow-clear />
			</a-form-item>
			<a-form-item label="用户ID：" name="userId">
				<a-input v-model:value="formData.userId" placeholder="请输入用户ID" allow-clear />
			</a-form-item>
			<a-form-item label="会话ID：" name="sessionId">
				<a-input v-model:value="formData.sessionId" placeholder="请输入会话ID" allow-clear />
			</a-form-item>
			<a-form-item label="父消息ID：" name="parentId">
				<a-input v-model:value="formData.parentId" placeholder="请输入父消息ID" allow-clear />
			</a-form-item>
			<a-form-item label="角色(user-用户,assistant-助手,system-系统)：" name="role">
				<a-input v-model:value="formData.role" placeholder="请输入角色(user-用户,assistant-助手,system-系统)" allow-clear />
			</a-form-item>
			<a-form-item label="消息内容：" name="content">
				<a-input v-model:value="formData.content" placeholder="请输入消息内容" allow-clear />
			</a-form-item>
			<a-form-item label="提示token数：" name="promptTokens">
				<a-input v-model:value="formData.promptTokens" placeholder="请输入提示token数" allow-clear />
			</a-form-item>
			<a-form-item label="补全token数：" name="completionTokens">
				<a-input v-model:value="formData.completionTokens" placeholder="请输入补全token数" allow-clear />
			</a-form-item>
			<a-form-item label="总token数：" name="totalTokens">
				<a-input v-model:value="formData.totalTokens" placeholder="请输入总token数" allow-clear />
			</a-form-item>
			<a-form-item label="状态（1-成功，2-失败）：" name="status">
				<a-input v-model:value="formData.status" placeholder="请输入状态（1-成功，2-失败）" allow-clear />
			</a-form-item>
			<a-form-item label="错误信息：" name="errorMessage">
				<a-input v-model:value="formData.errorMessage" placeholder="请输入错误信息" allow-clear />
			</a-form-item>
			<a-form-item label="排序：" name="sort">
				<a-input v-model:value="formData.sort" placeholder="请输入排序" allow-clear />
			</a-form-item>
			<a-form-item label="删除标志（0-未删除，1-已删除）：" name="delFlag">
				<a-input v-model:value="formData.delFlag" placeholder="请输入删除标志（0-未删除，1-已删除）" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="aiChatRecordForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import aiChatRecordApi from '@/api/ai/aiChatRecordApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				aiChatRecordApi
					.aiChatRecordSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
